package com.chucang.shucang.ttp.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


import static com.alipay.api.AlipayConstants.*;

/**
 * @author flitsneak
 * @email flitsneak@gmail.com
 * @date 2022/10/22 16:53
 * @description 支付宝支付配置
 */
@Configuration
public class AliPayConfig {

    @Value("${pay.alipay.privateKey}")
    private String privateKey;

    @Value("${pay.alipay.appId}")
    private String appId;

    @Value("${pay.alipay.serverUrl}")
    private String serverUrl;

    @Value("${pay.alipay.appCert}")
    private String appCert;

    @Value("${pay.alipay.aliCert}")
    private String aliCert;

    @Value("${pay.alipay.aliRootCert}")
    private String aliRootCert;


    @Bean
    public DefaultAlipayClient alipayClient() throws AlipayApiException {
        CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
        certAlipayRequest.setServerUrl(serverUrl);
        certAlipayRequest.setAppId(appId);
        certAlipayRequest.setPrivateKey(privateKey);
        certAlipayRequest.setFormat(FORMAT_JSON);
        certAlipayRequest.setCharset(CHARSET_UTF8);
        certAlipayRequest.setSignType(SIGN_TYPE_RSA2);
        certAlipayRequest.setCertPath(appCert);
        certAlipayRequest.setAlipayPublicCertPath(aliCert);
        certAlipayRequest.setRootCertPath(aliRootCert);

        return new DefaultAlipayClient(certAlipayRequest);
    }

}
